package org.dfrt.own.service.impl;

import org.dfrt.own.entity.User;
import org.dfrt.own.mapper.UserMapper;
import org.dfrt.own.service.UserService;

import org.dfrt.own.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhang
 * ClassName: UserServiceImpl
 * Description: 用户业务逻辑层
 * date: 2023/10/3 20:30
 * @since JDK 8
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 查找用户
     *
     * @param uname
     * @param upasswd
     * @return
     */
    @Override
    public User selectUserToLogin(String uname, String upasswd) {
        User user = new User();
        user.setUname(uname);
        user.setUpasswd(upasswd);
        User usera = userMapper.findUserByExample(user);
        return usera;
    }

    /**
     * 添加用户
     *
     * @param uname
     * @param passwd
     * @param repasswd
     * @return
     */
    @Override
    public Map<String, String> registerUser(String uname, String passwd, String repasswd) {

        Map<String, String> msgmap = new HashMap<>();

        if (uname == null) {
            msgmap.put("sta", "no");
            msgmap.put("msg", "用户名不能为空,请重新注册");
            return msgmap;
        }
        User user = new User();
        user.setUname(uname);
        User usera = userMapper.findUserByUser(user);
        if (usera != null) {
            msgmap.put("sta", "no");
            msgmap.put("msg", "用户已经存在,请重新注册");
            return msgmap;
        }
        if (passwd == null) {
            msgmap.put("sta", "no");
            msgmap.put("msg", "密码不能为空,请重新注册");
            return msgmap;
        }
        if (!CommonUtil.isValidPassword(passwd)) {
            msgmap.put("sta", "no");
            msgmap.put("msg", "密码格式不正确,请重新注册");
            return msgmap;
        }

        if (!passwd.equals(repasswd)) {
            msgmap.put("sta", "no");
            msgmap.put("msg", "两次密码不一致,请重新注册");
            return msgmap;
        }
        User userb = new User();
        userb.setUname(uname);
        userb.setUpasswd(passwd);

        System.out.println(userb);

        int i = userMapper.insertUser(userb);
        if (i > 0) {
            msgmap.put("sta", "yes");
            msgmap.put("msg", "注册成功，请登录");
        } else {
            msgmap.put("sta", "no");
            msgmap.put("msg", "注册失败，请重新注册");
        }
        return msgmap;
    }

    @Override
    public Map<String, String> updateUser(HttpSession session, Integer uid, String uname, String ubirthday, String udesc, String upasswd) {

        Map<String, String> msgmap = new HashMap<>();

        User user = new User();
        user.setUname(uname);
        User usera = userMapper.findUserByUserName(user);
        if (usera != null) {
            msgmap.put("sta", "no");
            msgmap.put("msg", "修改失败,用户名已经被占用");
            return msgmap;
        }
        if (!CommonUtil.isValidPassword(upasswd)) {
            msgmap.put("sta", "no");
            msgmap.put("msg", "修改失败,密码格式不正确");
            return msgmap;
        }
        User users = (User) session.getAttribute("user");

        User userb = new User(uid, uname, ubirthday, udesc, upasswd);
        if(users.equals(userb)){
            msgmap.put("sta", "yes");
            msgmap.put("msg", "您未进行任何修改");
            return msgmap;
        }

        int i = userMapper.updateUserInfo(userb);

        if (i > 0) {
//            if (!(uname.equals(userb.getUname())||upasswd.equals(userb.getUpasswd()))){
//                msgmap.put("sta", "yes");
//                msgmap.put("msg", "用户名或密码被修改请重新登录");
//                msgmap.put("uu", "yes");
//                return msgmap;
//            }
            msgmap.put("sta", "yes");
            msgmap.put("msg", "修改成功");
            session.setAttribute("user", userb);

        } else {
            msgmap.put("sta", "no");
            msgmap.put("msg", "修改失败");
        }

        return msgmap;
    }

    /**
     * 注销用户
     * @param user
     * @return
     */
    @Override
    public Map<String, String> deletUserByUser(User user) {
        Map<String, String> msgmap = new HashMap<>();


        int i = userMapper.deletUser(user);
        if (i > 0){
            msgmap.put("sta", "yes");
            msgmap.put("msg", "注销成功");
        }else {
            msgmap.put("sta", "no");
            msgmap.put("msg", "注销失败");
        }

        return msgmap;
    }
}
